/*
Dtao 2022.3.11
*/

// size_t timestamp_save; // 保留获取到的时间戳，可以放在eeprom，因为时间不能倒退
void timestamp2date(size_t timestamp) // 时间戳转化为时间
{   
    //timestamp_save = timestamp;
    timestamp -= 1640966400; // 将标准时间戳减去2022年1月1日0：00:00的时间戳，便于计算
    uint8_t hour = (timestamp % 86400) / 3600; // 计算时
    uint8_t minute = ((timestamp % 86400) % 3600) / 60; //计算分
    uint8_t second = (((timestamp % 86400) % 3600) % 60); // 计算秒
    system_time.hour = hour;
    system_time.minute = minute;
    system_time.second = second; // 同步系统时钟
    uint8_t month = 1;
    uint16_t year = 2022;
    uint8_t day = 1;

    for(; timestamp >= 86400; timestamp -= 86400) // 按日计算
    {
        day++;
        switch(month)
        {
            case  1:if(day == 32){day = 1;month++;};break;
            case  2:if(if_leap_year(year))
                {
                    if(day == 30)
                    {
                        day = 1;
                        month++;
                    }
                }
                else
                {
                    if(day == 29)
                    {
                        day = 1;
                        month++;
                    }
                }
                break;
            case  3:if(day == 32){day = 1;month++;};break;
            case  4:if(day == 31){day = 1;month++;};break;
            case  5:if(day == 32){day = 1;month++;};break;
            case  6:if(day == 31){day = 1;month++;};break;
            case  7:if(day == 32){day = 1;month++;};break;
            case  8:if(day == 32){day = 1;month++;};break;
            case  9:if(day == 31){day = 1;month++;};break;
            case 10:if(day == 32){day = 1;month++;};break;
            case 11:if(day == 31){day = 1;month++;};break;
            case 12:if(day == 32){day = 1;month++;};break;
            default :break;
        }    
        if(month == SYSTEM_TIME_MONTH_MAX)
        {
            month = 1;
            year++;
        }
    } // 算出年月日
    system_time.year = year;
    system_time.month = month;
    system_time.day = day;
}

//*********************TIME_CONVERT************************
system_time_struct system_time; // 用户时钟
void system_time_run(void) // 时间运行函数 可放进100ms定时中断
{
    system_time.millisecond += 100;
    if(system_time.millisecond == SYSTEM_TIME_MILLISECOND_MAX)
    {
        system_time.millisecond = 0;
        system_time.second++;
        
        if(system_time.second == SYSTEM_TIME_SECOND_MAX)
        {
            system_time.second = 0;
            system_time.minute++;
                
            if(system_time.minute == SYSTEM_TIME_MINUTE_MAX)
            {
                system_time.minute = 0;
                system_time.hour++;
        
                if(system_time.hour == SYSTEM_TIME_HOUR_MAX)
                {
                    system_time.hour = 0;
                    system_time.day++;

                    switch(system_time.month)
                    {
                        case  1:if(system_time.day == 32){system_time.day = 1;system_time.month++;};break;
                        case  2:if(if_leap_year(system_time.year))
                                {
                                    if(system_time.day == 30)
                                    {
                                        system_time.day = 1;
                                        system_time.month++;
                                    }
                                }
                                else
                                {
                                    if(system_time.day == 29)
                                    {
                                        system_time.day = 1;
                                        system_time.month++;
                                    }
                                }
                                break;
                        case  3:if(system_time.day == 32){system_time.day = 1;system_time.month++;};break;
                        case  4:if(system_time.day == 31){system_time.day = 1;system_time.month++;};break;
                        case  5:if(system_time.day == 32){system_time.day = 1;system_time.month++;};break;
                        case  6:if(system_time.day == 31){system_time.day = 1;system_time.month++;};break;
                        case  7:if(system_time.day == 32){system_time.day = 1;system_time.month++;};break;
                        case  8:if(system_time.day == 32){system_time.day = 1;system_time.month++;};break;
                        case  9:if(system_time.day == 31){system_time.day = 1;system_time.month++;};break;
                        case 10:if(system_time.day == 32){system_time.day = 1;system_time.month++;};break;
                        case 11:if(system_time.day == 31){system_time.day = 1;system_time.month++;};break;
                        case 12:if(system_time.day == 32){system_time.day = 1;system_time.month++;};break;
                        default :break;
                    }
                    if(system_time.month == 13)
                    {
                        system_time.month = 1;
                        system_time.year++;
                    }
                    
                }
            }
        }
    }
}
uint8_t if_leap_year(uint16_t input_year)
{
    if((((input_year % 4) == 0)&&((input_year % 100) != 0))||((input_year % 400) == 0))
        return 1;
    else
        return 0;
}
